#ifndef _DUDE_H_
#define _DUDE_H_ 

#include "render_target.h"
#include "sprite.h"
#include "sprite_sheet.h"
#include "dude_controller.h"

//What is the current visual state of dude? what effects should be turned on, etc.
enum DudeAppearance {
	DUDE_NORMAL,
	DUDE_DAMAGED,
	DUDE_DEAD,
	DUDE_INVISIBLE
};

//What is the dude currently doing? What animation are we in?
enum DudeAction {
	DUDE_NOTHING,
	DUDE_WALKING,
	DUDE_FIGHTING
};

//Holds all the numberical stats that describe a dude in battle terms 
typedef struct DudeStats {
	//Character stats
	Uint16 health, speed, offense, defense, range, damage; //attack, defense, level
	//Current status
	Sint16 life, xRoll;
	void setAll(Uint16 hlt, Uint16 spd, Uint16 off, Uint16 def, Uint16 rng, Uint16 dmg) { health = hlt; speed = spd; offense = off; defense = def; range = rng; damage = dmg; }
	Uint16 getDamage() { return 4 * damage + xRoll / 2; }
	Uint16 getAttackRoll(Uint16 yRoll) { return (offense + xRoll) * 3 / 2; }
	Uint16 getDefenseValue() { return 2 * defense + speed; }
	Uint16 getLifeDefault() { return health * 10; }
	void rollX() { xRoll = rand() % 10 + 1; }
	Uint16 getActionRate() {
		if (speed >= 9)
			return 1;
		else if (speed >= 7)
			return 2;
		else if (speed >= 4)
			return 3;
		else
			return 4;
	}
	Uint16 getBuildTime() { return (health + speed + offense + defense + range + damage) / 2; }
} DudeStats;

typedef struct DudeTemplate {
	DudeStats stats;
	std::string baseResourceName; //For example "Robot Man.jpg" would also look at "Robot Man_fighting.jpg", "Robot Man_movement.jpg".
	
} DudeTemplate;

/* Represents an individual unit on the battle field, with individual stats and life */
class Dude
{
protected:
	DudeController &controller;
public:
	RenderTarget* node; //Points to a rendertarget root node with several sub-nodes for the sprite, a shadow, any attached effects, etc.
	DudeStats stats; //My individual stats
	DudeAppearance appearance; //What visual state
	DudeAction action; //What currently doing
	PlayerSide side;
	Dude(DudeController &dudeController, PlayerSide playerSide, DudeTemplate &dudeTemplate);
	~Dude();
	PD_rect bounds;
	SDL_Surface* boundsImage;
	std::string baseResourceName;
	//Dude *nextDude, *prevDude; //Double linked list
	double xDir, dyingTime;
	int pathNumber;
	bool isDying;
	Sint8 actionCounter;
	Uint8 actionTime;

	static void staticSpriteAnimationCallback(void* obj, SpriteSheet* sprite);
	void SpriteAnimationCallback(SpriteSheet* sprite);
	SpriteSheet* CreateSpriteSheet();
	void InitAppearance(); //Builds node tree, based on state
	static void AlignSprite(Sprite* sprite);
	static double getXOverlap(PD_rect &movingRect, PD_rect &other, double xDir);
	void AttackEnemy(Dude *enemy);
	bool TakeDamage(Uint16 amount);
	void StartNothing();
	void StartWalking();
	void StartFighting();
	void StartDying();
	void updateBounds();
	void Heartbeat();
	//bool atFront();
	//Dude* getFront(); //Gets last dude in path
	//Dude* removeFromList(Dude* dude);
	//void clearLinks();
	bool atDestination();
	bool canMoveForward(double &amountCanMove);
	void moveBy(double x, double y);
	PD_vect2 getBoundsOffset();
	double getZ();
	void setZ(double z);
};

#endif